package com.chen.pspms.controller;

import com.chen.pspms.entity.CompanyInfo;
import com.chen.pspms.entity.Majorhazardregistertable;
import com.chen.pspms.entity.Resource;
import com.chen.pspms.entity.User;
import com.chen.pspms.service.CompanyService;
import com.chen.pspms.service.MajorhazardregisterService;
import com.chen.pspms.service.ResourceService;
import com.chen.pspms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/companyStaff")
public class CompanyStaffController {
    @Autowired
    CompanyService companyService;
    @Autowired
    MajorhazardregisterService majorhazardregisterService;
    @Autowired
    UserService userService;
    @Autowired
    ResourceService resourceService;

    @RequestMapping("/list")
    public String list(Model model, HttpSession session){
        List<Majorhazardregistertable> majorhazardregisterList=getMajorhazardregisterList(session);
        model.addAttribute("majorhazardregisterList",majorhazardregisterList);
        return "预警防控/企业责任落实预警";
    }

    @RequestMapping("/resource")
    public String resource(Model model,HttpSession session){
        List<Majorhazardregistertable> majorhazardregisterList=getMajorhazardregisterList(session);
        List<Resource> resourcelist= resourceService.geResourceList(majorhazardregisterList);
        System.out.println(resourcelist);
        model.addAttribute("resourcelist",resourcelist);
        return "预警防控/应急资源预警";
    }

    private List<Majorhazardregistertable> getMajorhazardregisterList(HttpSession session){
        //获取登陆用户
        User usert = (User)session.getAttribute("user");
        User user = userService.findyByAccount(usert.getAccount());
        //获取企业 id
        String companyName = user.getCompname();
        CompanyInfo company = companyService.findyByName(companyName);

        List<Majorhazardregistertable> allMajorhazardregisterList=majorhazardregisterService.findyListByComId(company.getId());
        //去除正常的
        return majorhazardregisterService.removeSafe(allMajorhazardregisterList);
    }

}
